home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com
- Newsgroups: comp.lang.c++
- Subject: Re: Trouble w/ random numbers...please help
- Date: 3 Feb 1996 23:35:39 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4f0rgb$4h7@news1.usa.pipeline.com>
- References: <4f0p3f$lon@netnews.upenn.edu>
- NNTP-Posting-Host: 38.8.61.12
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Feb 03, 1996 22:54:39 in article <Re: Trouble w/ random numbers...please
- help>, 'modavis@ssc.sas.upenn.edu (Morris A. Davis)' wrote:
-
-
- >Daniel Cotter (cotter@www.cns.uky.edu) wrote:
- >: I'm trying to write a program that will generate a set of
- >: random numbers between 3 and 18. I have several problems with
- >: this program.
- >: 1. C++ always generates same number(346)
- >
- >can't help ya
- >
- >: 2. I don't know how to give a range. I've tried 3-18, but
- >: it thinks I mean to subtract(3-18=15)
- >
- >1. Generate a random number between 0 and 1
- >2. Multiply by 15
- >3. Add 3
- >
- Here's a more helpful response:
- First, I assume you mean values 3 - 18 inclusive.
-
- #include <stdlib.h>
- #include <time.h>
-
- /* In the beginning of your program, initialize the random number
- generator - lest you'll always get the same series */
-
- srand( (unsigned)time( NULL ) );
-
- /* then, anywhere in your program you can do something
- along these lines: */
-
- int rn = rand() % 16 + 3;
-
- /* rn will now contain a pseudo random value 3-18. */
-
- --
- Pete Grant
- Kalevi, Inc.
- Object Oriented Software Development
-